package com.mlethe.android.demo.seattable.activity;

import android.os.Bundle;
import android.util.SparseArray;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.mlethe.android.demo.R;
import com.mlethe.android.demo.seattable.base.BaseActivity;
import com.mlethe.android.demo.seattable.dialog.LoadingDialog;
import com.mlethe.android.demo.seattable.entity.Hall;
import com.mlethe.android.demo.seattable.fragment.MovieSessionFragment;
import com.mlethe.android.demo.seattable.presenter.MovieSessionPresenter;
import com.mlethe.android.demo.seattable.view.MovieSessionView;
import com.mlethe.library.navigation.DefaultNavigationBar;
import com.mlethe.library.utils.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MovieSessionActivity extends BaseActivity<MovieSessionPresenter, MovieSessionView> implements MovieSessionView {

    private String playIds;

    private TabLayout tabLayout;

    private ViewPager viewPager;
    private MovieSessionAdapter adapter;

    private Map<String, ArrayList<Hall>> map = new HashMap<>();
    private List<String> mTitles = new ArrayList<>();

    private SparseArray<Fragment> fragmentSparseArray = new SparseArray<>();
    private LoadingDialog loadingDialog;

    @Override
    protected MovieSessionPresenter createPresenter() {
        return new MovieSessionPresenter(this);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_movie_session2);
    }

    @Override
    protected void initTitle() {
        new DefaultNavigationBar.Builder(this)
                .setBackgroundResource(R.color.colorPrimary)
                .setTitle("影城")
                .create();
        playIds = getIntent().getStringExtra("playIds");
    }

    @Override
    protected void initView() {
        tabLayout = (TabLayout) findViewById(R.id.movie_time_tab);
        tabLayout.post(new Runnable() {
            @Override
            public void run() {

            }
        });
        viewPager = (ViewPager) findViewById(R.id.movie_field_vp);
        adapter = new MovieSessionAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        // 设置预加载界面个数
        viewPager.setOffscreenPageLimit(0);
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    protected void initData() {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog();
            loadingDialog.setNoTitle(true);
        }
        if (!loadingDialog.isVisible()) {
            loadingDialog.showAllowingStateLoss(getSupportFragmentManager());
        }
        mPresenter.cinemaPlayInfo(playIds);

    }

    @Override
    public void success(Map<String, ArrayList<Hall>> result) {
        loadingDialog.dismiss();
        mTitles.clear();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("MM月dd日");
        for (Map.Entry<String, ArrayList<Hall>> entry: result.entrySet()) {
            try {
                Date date = sdf1.parse(entry.getKey());
                String str = "";
                if (date.equals(DateUtil.getDayBegin())) {
                    str += "今天";
                } else if (date.equals(DateUtil.getDayBegin(1, 0, 0))){
                    str += "明天";
                } else if (date.equals(DateUtil.getDayBegin(2, 0, 0))){
                    str += "后天";
                }
                str += sdf2.format(date);
                mTitles.add(str);
                map.put(str, entry.getValue());
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        adapter.notifyDataSetChanged();
        initTab();
    }

    @Override
    public void fail(String str) {

    }

    private void initTab() {
        tabLayout.removeAllTabs();
        for (int i = 0; i < mTitles.size(); i++) {
            tabLayout.addTab(tabLayout.newTab().setText(mTitles.get(i)));
        }
    }

    private class MovieSessionAdapter extends FragmentPagerAdapter {
        public MovieSessionAdapter(FragmentManager fm) {
            super(fm);
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles.get(position);
        }

        @Override
        public Fragment getItem(int i) {
            Fragment fragment = fragmentSparseArray.get(i);
            if (fragment == null) {
                fragment = new MovieSessionFragment();
                Bundle bundle = new Bundle();
                bundle.putParcelableArrayList("halls", map.get(mTitles.get(i)));
                fragment.setArguments(bundle);
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return mTitles.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//            super.destroyItem(container, position, object);
        }
    }
}
